package com.yinet.web.controller.touchscreen;

import com.alibaba.fastjson.JSON;
import com.yinet.common.config.Global;
import com.yinet.common.config.ServerConfig;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.core.domain.Ztree;
import com.yinet.common.utils.ServletUtils;
import com.yinet.common.utils.StringUtils;
import com.yinet.common.utils.file.FileUploadUtils;
import com.yinet.common.utils.http.HttpUtils;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.service.TouchScreenGuidanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 办事指南信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/touchscreen/guidance")
public class GuidanceAdminController extends BaseController {
    static String prefix = "touchscreen/admin/guidance";

    @Autowired
    private TouchScreenGuidanceService guidanceService;

    @GetMapping()
    public String guidance() {
        return prefix + "/list";
    }

    /**
     * 获取办事指南列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public List<TouchScreenGuidance> list() {
        List<TouchScreenGuidance> guidanceList = guidanceService.selectGuidanceList();
        return guidanceList;
    }

    /**
     * 新增办事指南
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        TouchScreenGuidance guidance = null;
        if (0L != id) {
            guidance = guidanceService.selectGuidanceById(id);
        } else {
            guidance = new TouchScreenGuidance();
            guidance.setTsGuidanceId(0L);
            guidance.setGuidanceName("根目录");
        }
        mmap.addAttribute("guidanceNode", guidance);
        return prefix + "/add";
    }

    /**
     * 新增保存办事指南
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TouchScreenGuidance guidance) {
        int i = guidanceService.insertGuidance(guidance);
        return toAjax(i);
    }

    /**
     * 编辑办事指南
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.addAttribute("guidanceNode", guidanceService.selectGuidanceById(id));
        return prefix + "/edit";
    }

    /**
     * 编辑保存办事指南
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TouchScreenGuidance guidance) {
        if (guidance.getParentId().equals(0l) && !guidance.getGuidanceType().equals("M")) {
            return AjaxResult.warn("主目录不允许更改为办事指南项");
        } else if (guidanceService.selectCountGuidanceByParentId(guidance.getTsGuidanceId()) > 0 && !guidance.getGuidanceType().equals("M")) {
            return AjaxResult.warn("存在子项,不允许更改指南类型");
        }
        int i = guidanceService.updateGuidance(guidance);
        return toAjax(i);
    }

    /**
     * 删除菜单
     */
    @GetMapping("/remove/{guidanceId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("guidanceId") Long guidanceId) {
        if (guidanceService.selectCountGuidanceByParentId(guidanceId) > 0) {
            return AjaxResult.warn("存在子项,不允许删除");
        }
        return toAjax(guidanceService.deleteGuidanceById(guidanceId));
    }

    @PostMapping("/uploadDoc")
    @ResponseBody
    public String fileUpload(@RequestParam("docFile") MultipartFile file) {
        TouchScreenGuidance guidance = new TouchScreenGuidance();
        guidance.setDocName(file.getOriginalFilename());
        try {
            String upload = FileUploadUtils.upload(Global.getGuidanceDocPath(), file);
            guidance.setDocUrl(upload);
        } catch (IOException e) {
            return "上传失败";
        }
        System.out.println("文件名称：" + guidance.getDocName());
        System.out.println("上传路径：" + guidance.getDocUrl());
        return JSON.toJSONString(guidance);
    }

    @PostMapping("/uploadIcon")
    @ResponseBody
    public String iconFileUpload(@RequestParam("iconFile") MultipartFile file) {
        String iconUrl;
        try {
            iconUrl = FileUploadUtils.upload(Global.getGuidanceIconPath(), file);
        } catch (IOException e) {
            return "上传失败";
        }
        System.out.println("上传路径：" + iconUrl);
        return "{\"iconUrl\":\"" + iconUrl + "\"}";
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/guidanceTreeData")
    @ResponseBody
    public List<Ztree> guidanceTreeData() {
        List<Ztree> ztrees = guidanceService.guidanceTreeData();
        return ztrees;
    }

    /**
     * 选择办事指南树
     */
    @GetMapping("/selectGuidanceTree/{id}")
    public String selectGuidanceTree(@PathVariable("id") Long id, ModelMap mmap) {
        TouchScreenGuidance guidance = null;
        if (0L != id) {
            guidance = guidanceService.selectGuidanceById(id);
        } else {
            guidance = new TouchScreenGuidance();
            guidance.setTsGuidanceId(0L);
            guidance.setGuidanceName("根目录");
        }
        mmap.put("guidance", guidance);
        return prefix + "/tree";
    }

//    /**
//     * 新增保存办事指南
//     */
//    @GetMapping("/testdb")
//    @ResponseBody
//    public String testDb() throws IOException {
//        String msg = guidanceService.testGuidance();
//        return msg;
//    }
}